package IOBaseTest;

import java.io.File;
import java.io.FilenameFilter;

/**
 * Created by yexuan on 2018/3/15.
 * 获得指定目录下文件
 */
public class FileDemo {
    public static void main(String[] args) {
        getFile3();
    }

    //递归查出目录下所有内容
    public static void getFile3(){
        File f = new File("f://soft//moniTool");
        show(f);

    }
    public static void show(File f){
        System.out.println(f);
        File[] fs = f.listFiles();
        for (int i = 0; i < fs.length; i++) {
            if(fs[i].isDirectory()){
                show(fs[i]);
            }
            System.out.println(fs[i]);
        }
    }

    public static void getFile2(){
        File f = new File("f://soft");  //该目录必须存在
        //FilenameFilter：文件过滤  得到后缀名为txt的文件
        String[] fs = f.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith("txt");
            }
        });
        System.out.println(fs.length);
        for (String s: fs) {
            System.out.println(s);
        }
    }

    public static void getFile1(){
        File f = new File("f://soft");  //该目录必须存在
        String[] fs = f.list();
        System.out.println(fs.length);
        for (String s: fs) {
            System.out.println(s);
        }
    }
}
